package com.swg.controller.backend;

import com.github.pagehelper.PageInfo;
import com.swg.common.Constants;
import com.swg.common.ResponseEnum;
import com.swg.common.ServerResponse;
import com.swg.pojo.User;
import com.swg.service.OrderService;
import com.swg.service.UserService;
import com.swg.vo.OrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @Author 【swg】.
 * @Date 2018/1/7 14:09
 * @DESC
 * @CONTACT 317758022@qq.com
 */
@Controller
@RequestMapping("/manage/order/")
public class OrderManageController {
    @Autowired
    private UserService userService;
    @Autowired
    private OrderService orderService;

    @RequestMapping("list.do")
    @ResponseBody
    public ServerResponse<PageInfo> orderList(HttpSession session,
                                              @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                              @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize){
        User user = (User)session.getAttribute(Constants.CURRENT_USER);
        if(user==null){
            return ServerResponse.createByErrorCodeMessage(ResponseEnum.NEED_LOGIN.getCode(),"用户未登陆，请登录");
        }
        if(userService.checkAdminRole(user).isSuccess()){
            return orderService.manageList(pageNum,pageSize);
        }else {
            return ServerResponse.createByErrorMessage("需要管理员权限");
        }
    }

    @RequestMapping("detail.do")
    @ResponseBody
    public ServerResponse<OrderVo> orderDetail(HttpSession session, Long orderNo){
        User user = (User)session.getAttribute(Constants.CURRENT_USER);
        if(user==null){
            return ServerResponse.createByErrorCodeMessage(ResponseEnum.NEED_LOGIN.getCode(),"用户未登陆，请登录");
        }
        if(userService.checkAdminRole(user).isSuccess()){
            return orderService.manageDetail(orderNo);
        }else {
            return ServerResponse.createByErrorMessage("需要管理员权限");
        }
    }

    @RequestMapping("search.do")
    @ResponseBody
    public ServerResponse<PageInfo> orderSearch(HttpSession session, Long orderNo,
                                               @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                               @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize){
        User user = (User)session.getAttribute(Constants.CURRENT_USER);
        if(user==null){
            return ServerResponse.createByErrorCodeMessage(ResponseEnum.NEED_LOGIN.getCode(),"用户未登陆，请登录");
        }
        if(userService.checkAdminRole(user).isSuccess()){
            return orderService.manageSearch(orderNo,pageNum,pageSize);
        }else {
            return ServerResponse.createByErrorMessage("需要管理员权限");
        }
    }

    @RequestMapping("send_goods.do")
    @ResponseBody
    public ServerResponse<String> sendGoods(HttpSession session, Long orderNo){
        User user = (User)session.getAttribute(Constants.CURRENT_USER);
        if(user==null){
            return ServerResponse.createByErrorCodeMessage(ResponseEnum.NEED_LOGIN.getCode(),"用户未登陆，请登录");
        }
        if(userService.checkAdminRole(user).isSuccess()){
            return orderService.sendGoods(orderNo);
        }else {
            return ServerResponse.createByErrorMessage("需要管理员权限");
        }
    }

}
